<?php

namespace app\api\model;

use addons\epay\library\Service;
use app\admin\model\User;
use think\Exception;
use think\Model;
use Yansongda\Pay\Pay;

/**
 * 保温箱订单
 */
class IncubatorOrder extends Model
{

    // 表名
    protected $name = 'incubator_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function unfreeze($user_id,$refund_type)
    {
        $wechatUser = User::get($user_id);

        if ($wechatUser->is_incubator==0)throw new Exception("该用户保温箱已解冻或未购买保温箱");

        $money = $wechatUser->incubator_deposit;//押金

        $order = IncubatorOrder::get($wechatUser->incubator_order_id);

        if ($refund_type==0){
            //退余额
            \app\common\model\User::money($money,$user_id,'解冻保温箱押金:'.$money);
        } else {
            //原路退回
            if ($order->pay_type=='balance'){
                \app\common\model\User::money($money,$user_id,'解冻保温箱押金:'.$money);
            } else {
                $config = Service::getConfig('wechat');
                $response = Pay::wechat($config)->refund([
                    'type'          => 'miniapp',
                    'out_trade_no'  => $order['order_no'],
                    'out_refund_no' => time(),
                    'total_fee'     => $order['price'] * 100,
                    'refund_fee'    => $order['price'] * 100
                ]);
                if (!$response['result_code'] || $response['result_code'] != 'SUCCESS') {
                    throw new \Exception(($response['err_code'] ?? '') . ':' . ($response['err_code_des'] ?? '未知退款错误'));
                }
            }
        }

        $wechatUser->incubator_order_id = 0;

        $wechatUser->is_incubator = 0;

        $wechatUser->incubator_deposit = 0;

        $wechatUser->incubator_num = 0;
        $wechatUser->can_print = 0;
        $res = $wechatUser->save();

        return $res;
    }

    public function add($user_id,$num)
    {
        $model = new IncubatorOrder();

        $incubator_deposit = \app\common\model\Config::get(['name'=>'incubator_deposit'])->value;

        $data = [
            'user_id'        => $user_id,
            'price'          => $incubator_deposit*$num,
            'order_no'       => $this->order_no(),
            'num'            => $num
        ];


        //$wechatUser = User::get($user_id);
        $model->data($data);
        $res =  $model->save();
        return $model;
        /*if ($res){
            $request = request();
            $notifyurl =  $request->root(true)."/api/incubator/incubatornotifyx";

            $pay = \addons\epay\library\Service::submitOrder($data['price'],$data['order_no'],'wechat','保温箱押金',$notifyurl,'','miniapp',$wechatUser['wechat_open_id']);
            return $pay;
        } else {
            return false;
        }*/
    }




    public function order_no(){
        $order_no = null;
        while (true) {
            $order_no = date('YmdHis') . mt_rand(1000, 9999);
            $exist_order_no = IncubatorOrder::get(['order_no' => $order_no]);
            if (!$exist_order_no) {
                break;
            }
        }
        return $order_no;
    }

}
